function t = correltstat(r, n)
% correltstat  - convert correlation value into t-statistic
%
% FORMAT:       t = correltstat(r, n)
%
% Input fields:
%
%       r           correlation values (0 ... 1)
%       n           number of points in correlated series
%
% Output fields:
%
%       t           matching t-statistic

% Version:  v0.7f
% Build:    8110521
% Date:     Nov-05 2008, 9:00 PM CET
% Author:   Jochen Weber, SCAN Unit, Columbia University, NYC, NY, USA
% URL/Info: http://wiki.brainvoyager.com/BVQXtools

% argument check
if nargin < 2 || ...
   ~isnumeric(r) || ...
   ~isnumeric(n) || ...
    isempty(r) || ...
    isempty(n) || ...
    any(isnan(r(:)) | isinf(r(:))) || ...
    any(isinf(n(:)) | isnan(n(:)) | n(:) < 2)
    error( ...
        'BVQXtools:BadArgument', ...
        'Missing or invalid argument given.' ...
    );
end
osize = size(r);
if numel(r) == 1 && ...
    numel(n) > 1
    osize = size(n);
end
if ~isa(r, 'double')
    r = double(r(:));
else
    r = r(:);
end
if ~isa(n, 'double')
    n = double(n(:));
else
    n = n(:);
end

% delete invalid entries
r(abs(r) > 1) = 0;

% formula
t = reshape(r .* sqrt((n - 2) ./ (1 - r .^ 2)), osize);
